<?php
namespace app\admin\controller;
use think\Request;
use app\base\model\{
    Slide as SlideModel,
    SlideCate
};
class Slide extends Common{
    //轮播图分类模型
    protected $slideCate;
    //轮播图模型
    protected $slide;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->slide = new SlideModel();
        $this->slideCate = new SlideCate();
    }
    /**
     *添加轮播图分类
     * return json
     */
    public function addSlideCate(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $cate_name = $this->request->post('cate_name');
        $desc      = $this->request->post('desc');
        $status    = $this->request->post('status',1);
        if(empty($cate_name)){
            $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $count = $this->slideCate->where(['cate_name'=>$cate_name])->count();
        if($count>0){
            $msg['message']=checkMsg(CATE_IS_EXITE);
            return $msg;
        }
        $data=[
            'cate_name'  =>$cate_name,
            'status'      =>$status,
            'desc'       =>$desc,
            'add_time'   =>time()
        ];
        $res=$this->slideCate->insertGetId($data);
        if($res){
            $msg['message']=checkMsg(ADD_SUCCESS);
        }else{
            $msg['message']=checkMsg(ADD_FAIL);
        }
        return $msg;
     }
     /**
      *轮播图分类列表
      * return json
      */
     public function slideCateList(){
         if(empty($this->adminInfo)){
             $msg['message']=checkMsg(LOGIN_ERROR);
             return $msg;
         }
         $type = $this->request->post('type',0); //0:所有 1:只取显示的分类
         $where=[];
         if($type){
             $where['status']=1;
         }
         $list = $this->slideCate->order('cate_id','desc')->where($where)->select();
         if(!empty($list)){
             $msg['message']=checkMsg(GET_DATA_SUCCESS);
             $msg['list']=$list;
         }else{
             $msg['message']=checkMsg(GET_DATA_FAILE);
         }
         return $msg;
     }
    /**
     *分类信息
     * return json
     */
    public function slideCateInfo(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $cate_id = $this->request->post('cate_id');
        if(empty($cate_id)){
            $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $info = $this->slideCate->where(['cate_id'=>$cate_id])->find();
        if(empty($info)){
            $msg['message']=checkMsg(GET_DATA_FAILE);
        }else{
            $msg['message']=checkMsg(GET_DATA_SUCCESS);
            $msg['info']=$info;
        }
        return $msg;
    }
    /**
     *修改轮播图分类
     * return json
     */
    public function editeSlideCate(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $cate_name = $this->request->post('cate_name');
        $cate_id   = $this->request->post('cate_id');
        $status    =$this->request->post('status');
        $desc      =$this->request->post('desc');
        if(empty($cate_id) || empty($cate_name)){
            $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $count = $this->slideCate->where(['cate_name'=>$cate_name])->count();
        if($count>0){
            $msg['message']=checkMsg(CATE_IS_EXITE);
            return $msg;
        }
        $save_data=[
            'cate_name'  =>$cate_name,
            'desc'       =>$desc,
            'status'     =>$status
        ];
        $res = $this->slideCate->save($save_data,['cate_id'=>$cate_id]);
        if($res){
            $msg['message']=checkMsg(UPDATE_DATA_SUCCESS);
        }else{
            $msg['message']=checkMsg(UPDATE_DATA_FAIL);
        }
       return $msg;
    }
    /**
     *删除轮播图分类
     * return json
     */
  public function deleteSlideCate(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $cate_id = $this->request->post('cate_id');
      if(empty($cate_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $count = $this->slideCate->where(['cate_id'=>$cate_id])->count();
      if($count<1){
          $msg['message']=checkMsg(CATE_NOT_EXIT);
          return $msg;
      }
      $slideCount = $this->slide->where(['cate_id'=>$cate_id])->count();
      if($slideCount>0){
          $msg['message']=checkMsg(CATE_HAS_SLIDE);
          return $msg;
      }
     $res = $this->slideCate->where(['cate_id'=>$cate_id])->delete();
      if($res){
          $msg['message']=checkMsg(DELETE_DATA_SUCCESS);
      }else{
          $msg['message']=checkMsg(DELETE_DATA_FAIL);
      }
      return $msg;
  }
    /**
     *修改轮播图分类的状态
     * return json
     */
    public function editeCateStatus(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $cate_id = $this->request->post('cate_id');
        $status  = $this->request->post('status',0); //1:显示 0:不显示
        if(empty($cate_id)){
            $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $count = $this->slideCate->where(['cate_id'=>$cate_id])->count();
        if($count<1){
            $msg['message']=checkMsg(CATE_NOT_EXIT);
            return $msg;
        }
        $res = $this->slideCate->save(['status'=>$status],['cate_id'=>$cate_id]);
        if($res){
            $msg['message']=checkMsg(UPDATE_DATA_SUCCESS);
        }else{
            $msg['message']=checkMsg(UPDATE_DATA_FAIL);
        }
        return $msg;
    }

  /**
   *添加或修改轮播图
   * return json
   */
   public function addOrEditeSlide(){
       if(empty($this->adminInfo)){
           $msg['message']=checkMsg(LOGIN_ERROR);
           return $msg;
       }
       $cate_id = $this->request->post('cate_id');
       $remark  = $this->request->post('remark');
       $href    = $this->request->post('href');
       $is_play = $this->request->post('is_play',1);
       $path    = $this->request->post('image_path');
       $slide_id =$this->request->post('slide_id');
       if(empty($cate_id) || empty($path)){
            $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
       }
       $data=[
           'cate_id'  =>$cate_id,
           'href'     =>$href,
           'is_play'  =>$is_play,
           'path'     =>$path,
           'remark'   =>$remark
       ];

       if(empty($slide_id)){
           $data['add_time']=time();
           $res = $this->slide->insertGetId($data);
       }else{
           $res = $this->slide->save($data,['slide_id'=>$slide_id]);
       }
       if($res){
           $msg['message']=checkMsg(OPERA_SUCCESS);
       }else{
           $msg['message']=checkMsg(OPERA_FAIL);
       }
       return $msg;
   }
   /**
    *轮播图信息
    * return json
    */
   public function slideInfo(){
       if(empty($this->adminInfo)){
           $msg['message']=checkMsg(LOGIN_ERROR);
           return $msg;
       }
       $slide_id = $this->request->post('slide_id');
       if(empty($slide_id)){
           $msg['message']=checkMsg(CANSHU_ERROR);
           return $msg;
       }
       $info = $this->slide->where(['slide_id'=>$slide_id])->find();
       if(empty($info)){
           $msg['message']=checkMsg(GET_DATA_FAILE);
       }else{
           $msg['message']=checkMsg(GET_DATA_SUCCESS);
           $msg['info']=$info;
       }
       return $msg;
   }
   /**
    *修改轮播图的状态
    * return json
    */
   public function editeSlideStatus(){
       if(empty($this->adminInfo)){
           $msg['message']=checkMsg(LOGIN_ERROR);
           return $msg;
       }
       $slide_id = $this->request->post('slide_id');
       $is_play  = $this->request->post('is_play',0); //1:显示 0:不显示
       if(empty($slide_id)){
           $msg['message']=checkMsg(CANSHU_ERROR);
           return $msg;
       }
       $count = $this->slide->where(['slide_id'=>$slide_id])->count();
       if($count<1){
           $msg['message']=checkMsg(SLIDE_NOT_EXIST);
           return $msg;
       }
       $res = $this->slide->save(['is_play'=>$is_play],['slide_id'=>$slide_id]);
       if($res){
           $msg['message']=checkMsg(UPDATE_DATA_SUCCESS);
       }else{
           $msg['message']=checkMsg(UPDATE_DATA_FAIL);
       }
       return $msg;
   }
   /**
    *删除轮播图
    * return json
    */
   public function deleteSlide(){
       if(empty($this->adminInfo)){
           $msg['message']=checkMsg(LOGIN_ERROR);
           return $msg;
       }
       $slide_id = $this->request->post('slide_id');
       if(empty($slide_id)){
           $msg['message']=checkMsg(CANSHU_ERROR);
           return $msg;
       }
       $count = $this->slide->where(['slide_id'=>$slide_id])->count();
       if($count<1){
           $msg['message']=checkMsg(SLIDE_NOT_EXIST);
           return $msg;
       }
       $res = $this->slide->where(['slide_id'=>$slide_id])->delete();
       if($res){
          $msg['message']=checkMsg(DELETE_DATA_SUCCESS);
       }else{
           $msg['message']=checkMsg(DELETE_DATA_FAIL);
       }
       return $msg;
   }
}